import socket ,sys

def getaddrinfo_pre(host,port,sock_type,familypreference = socket.AF_INET):
    """get a addrinfo"""

    results = socket.getaddrinfo(host,port,0,sock_type)
    for result in results:
    	if result[0] == socket.AF_INET:
		return result
	return results[0]

host = sys.argv[1]
port = 'http'

c = getaddrinfo_pre(host,port,socket.SOCK_STREAM)
print "Connecting to ",c[4]
s = socket.socket(c[0],c[1])
try:
	s.connect(c[4])
except socket.gaierror,e:
	print "can not connect to ...",c[4]
	print "error > \n",e
	
s.sendall("HEAD / HTTP/1.0\n\n")

while True :
	s.settimeout(6)
	try:
		buf  = s.recv(4096)
    		if not len(buf):
		    break
		sys.stdout.write(buf)
	except socket.timeout:
	    print "time out ....."
	    sys.exit(0)
	except :
	    traceback.print_exc()

    		
